/*
 * Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://mindorks.com/license/apache-v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package org.changs.game.android.data.network;


import android.support.annotation.Nullable;

import com.google.gson.reflect.TypeToken;
import com.rx2androidnetworking.Rx2AndroidNetworking;

import org.changs.game.android.data.network.model.ApiRes;
import org.changs.game.android.data.network.model.BookDetail;
import org.changs.game.android.data.network.model.BookResponse;
import org.changs.game.android.data.network.model.ChildMusicListResponse;
import org.changs.game.android.data.network.model.PushMusicCreatedRequest;
import org.changs.game.android.di.DeviceId;
import org.changs.game.android.utils.JSONObject;

import java.io.File;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Single;

@Singleton
public class AppApiHelper implements ApiHelper {

    private final String deviceId;

    @Inject
    public AppApiHelper(@DeviceId String deviceId) {
        this.deviceId = deviceId;
    }

    @Override
    public Single<ApiRes<List<BookResponse>>> checkBookVersion(
            @Nullable String version) {
        JSONObject params = new JSONObject();
        if (version == null) {
            version = "0";
        }
        params.put("version", version);
        return Rx2AndroidNetworking.post(ApiEndPoint.BOOK_FEATURE_CHECK)
                .addJSONObjectBody(params)
                .build()
                .getParseSingle(new TypeToken<ApiRes<List<BookResponse>>>() {
                });
    }

    @Override
    public Single<ApiRes<BookDetail>> getBookDetail(String isbn) {
        JSONObject params = new JSONObject();
        params.put("isbn", isbn);
        return Rx2AndroidNetworking.post(ApiEndPoint.BOOK_GET)
                .addJSONObjectBody(params)
                .build()
                .getParseSingle(new TypeToken<ApiRes<BookDetail>>() {
                });
    }

    @Override
    public Single<ApiRes<String>> uploadFile(File file) {
        return Rx2AndroidNetworking.upload(ApiEndPoint.FILE_UPLOAD)
                .addMultipartFile("file", file)
                .build()
                .getParseSingle(new TypeToken<ApiRes<String>>() {
                });
    }

    @Override
    public Single<ApiRes> messageSend(String attachUrl) {
        JSONObject params = new JSONObject();
        params.put("deviceId", deviceId);
        params.put("attachUrl", attachUrl);
        params.put("attachType", "video");
        return Rx2AndroidNetworking.post(ApiEndPoint.SEND_MESSAGE)
                .addJSONObjectBody(params)
                .build()
                .getParseSingle(new TypeToken<ApiRes>() {
                });
    }

    @Override
    public Single<ApiRes> createChildMusicFavo(PushMusicCreatedRequest request){
        JSONObject params = new JSONObject();
        params.put("deviceId", request.getDeviceId());
        params.put("musicName", request.getMusicName());
        params.put("musicUrl", request.getMusicUrl());
        params.put("operId",request.getOperId());
        params.put("thirdId",request.getThirdId());
        params.put("tip",request.getTip());
        return Rx2AndroidNetworking.post(ApiEndPoint.CHILDMUSICFAVO_CREATE)
                .addJSONObjectBody(params)
                .build()
                .getParseSingle(new TypeToken<ApiRes>() {
                });
    }

    @Override
    public Single<ApiRes<ChildMusicListResponse>> queryChildMusicList(String childId){
        JSONObject params = new JSONObject();
        params.put("page",1);
        params.put("size",1000);
        params.put("deviceId", childId);
        return Rx2AndroidNetworking.post(ApiEndPoint.CHILDMUSICFAVO_QUERY)
                .addJSONObjectBody(params)
                .build()
                .getParseSingle(new TypeToken<ApiRes<ChildMusicListResponse>>() {
                });
    }
}

